package com.example.onlineeducation.viewmodel


import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.onlineeducation.base.BaseViewModel
import com.example.onlineeducation.data.entity.ProblemEntity
import com.example.onlineeducation.data.json.OnlineExaminationFinishJson
import com.example.onlineeducation.repository.OnlineRepository
import com.yc.jetpacklib.net.YcResult
import kotlinx.coroutines.flow.collectLatest


/**
 * Creator: yc
 * Date: 2021/6/15 18:03
 * UseDes:
 */
class OnlineExaminationAcVM : BaseViewModel() {

    private val mOfflineRepository: OnlineRepository by lazy { OnlineRepository(mApiService) }

    private val _mGetExaminationProblemList = MutableLiveData<YcResult<List<ProblemEntity>?>>()
    val mGetExaminationProblemList: LiveData<YcResult<List<ProblemEntity>?>> = _mGetExaminationProblemList

    fun getExaminationProblemList(examinationId: String) = ycLaunch {
        mOfflineRepository.getExaminationProblemList(examinationId).ycHasLoading().collectLatest { result ->
            _mGetExaminationProblemList.postValue(result)
        }
    }

    private val _mFinishExamination = MutableLiveData<YcResult<OnlineExaminationFinishJson>>()
    val mFinishExamination: LiveData<YcResult<OnlineExaminationFinishJson>> = _mFinishExamination

    /**
     * 完成线上考试
     * @param proId String                      项目id
     * @param examinationId String              考试id
     * @param problemList List<ProblemEntity>   考题结果
     */
    fun finishExamination(proId: String, examinationId: String, problemList: List<ProblemEntity>) = ycLaunch {
        mOfflineRepository.getExaminationFinish(proId, examinationId, problemList).ycHasLoading().collectLatest { result ->
            _mFinishExamination.postValue(result)
        }

    }
}


